package com.deng.Service.impl;

import com.deng.Service.CommentsService;
import com.deng.Service.PaperService;
import com.deng.Service.UserService;
import com.deng.config.ShowComments;
import com.deng.config.common.Paging;
import com.deng.config.common.TimeUtil;
import com.deng.mapper.ColumnMapper;
import com.deng.mapper.CommentsMapper;
import com.deng.model.Column;
import com.deng.model.Comments;
import com.deng.model.Ex.CommentsWithPaperMes;
import com.deng.model.Paper;
import com.deng.model.User;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * @Author 邓小熙
 * @Date 2022/5/10 17:07
 * @Version 1.0
 */
@Service
public class CommentsServiceImpl implements CommentsService {
    @Autowired
    CommentsMapper commentsMapper;
    @Autowired
    PaperService paperService;
    @Autowired
    UserService userService;

    /**
     * 通过时间顺序将评论排序
     * @return
     */
    @Override
    public Map<String, Object> findAllCommentsOrderByTime() {
        Map<String,Object> map = Maps.newHashMap();
//      这里应该统一控制页面
        List<Comments> comments = commentsMapper.findAllCommentsOrderByTime(0, Paging.LIMIT.getValue());
        List<CommentsWithPaperMes> commentsWithPaperMesList = setCommentsWithPaperAndUserMes(comments);
        System.out.println(commentsWithPaperMesList);
        map.put("comments",commentsWithPaperMesList);
        return map;

}

    private List<CommentsWithPaperMes> setCommentsWithPaperAndUserMes(List<Comments> list) {
        List<CommentsWithPaperMes> commentsWithPaperMesList = Lists.newLinkedList();
        for (Comments comments: list
        ) {
            CommentsWithPaperMes commentsWithPaperMes = new CommentsWithPaperMes();

//          Paper paper = paperService.findPaperById(comments.getComment_paper());
            Paper paper = paperService.findPaperById(comments.getComments_paper());
            User user = userService.findUsersById(comments.getComments_user());

            commentsWithPaperMes.setComment_id(comments.getId());
            commentsWithPaperMes.setComments_content(comments.getComments_content());
            commentsWithPaperMes.setComments_time(comments.getComments_time());
            commentsWithPaperMes.setComments_user_id(user.getId());
            commentsWithPaperMes.setComments_user_name(user.getLogin_name());
            commentsWithPaperMes.setPaper_id(paper.getId());
            commentsWithPaperMes.setPaper_image(paper.getPaper_image());
            commentsWithPaperMes.setTitle(paper.getTitle());
            commentsWithPaperMesList.add(commentsWithPaperMes);
        }
        System.out.println(commentsWithPaperMesList);
        return commentsWithPaperMesList;
    }

    /**
     *
     * 通过文章ID查找评论的数量
     * @param id
     * @return
     */
    @Override
    public int findCommentCountByPaperId(int id) {
        return commentsMapper.findCommentCountByPaperId(id);
    }
    /**
     * 通过文章ID查找所有相应文章的评论
     * @param id
     * @return
     */
    @Override
    public Map<String, Object> findAllCommentsByPaperId(int id) {
        Map<String,Object> map = Maps.newHashMap();
        List<Comments> comments = commentsMapper.findAllCommentsByPaperId(id);
        List<ShowComments> list= setValue(comments);
        map.put("code",0);
        map.put("count",30);
        map.put("mes","");
        map.put("data",list);
        return map;
    }

    /**
     *
     * @param list
     * @return
     */
    private List<ShowComments> setValue(List<Comments> list){
        List<ShowComments> showCommentsList = Lists.newLinkedList();
        for (Comments comment : list) {
            ShowComments showComments = new ShowComments();
            showComments.setId(comment.getId());
            showComments.setContent(comment.getComments_content());
            showComments.setTime(comment.getComments_time());
            showComments.setTitle(paperService.findPaperTitleById(comment.getComments_paper()));
            showCommentsList.add(showComments);
        }
        return showCommentsList;
    }
    /**
     * 通过用户ID查找用户所有文章的评论
     * @param userName
     * @return
     */
    @Override
    public Map<String, Object> findCommentsByUserId(String userName) {
        int userId = userService.findAuthorByName(userName);
        List<Comments> list = commentsMapper.findCommentsByUserId(userId,Paging.LIMIT.getValue());
//        List<Comments> list = commentsMapper.findCommentsByUserId(userId,Paging.LIMIT.getValue());
        List<CommentsWithPaperMes> commentsWithPaperMesList = setCommentsWithPaperAndUserMes(list);
        Map<String,Object> map = Maps.newHashMap();
        map.put("list",commentsWithPaperMesList);
        return map;
    }
    /**
     * 增加评论
     * @param userName
     * @param paperId
     * @param userName
     * @return
     */
    @Override
    public int insertComments(String content, int paperId,String userName) {

        Timestamp timestamp = TimeUtil.setCurrentTime();
        Comments comments = new Comments();
        comments.setComments_content(content);
        comments.setComments_paper(paperId);
        comments.setComments_user(userService.findAuthorByName(userName));
        comments.setComments_time(timestamp);
        return commentsMapper.insertComments(comments);
    }

    /**
     * 通过ID删除评论
     * @param id
     * @return
     */
    @Override
    public int deleteCommentById(int id) {
        return commentsMapper.deleteCommentById(id);
    }

}
